//--helper--
#include "preprocessor_mode.h"

void preprocessor_mode( std::ostringstream & osstream
                  , quex::depythonize_c_lex & qlex
                  , quex::Token & token
                  , quex::Token & tokenNext
                  , std::vector<quex::Token> & tokens_before_token
                  , std::vector<quex::Token> & tokens_before_tokenNext
                  )
  bool multiline_continue = false
  bool not_in_parentheses_block = true

  while(token.type_id() != QUEX_TKN_TERMINATION &&
        token.type_id() != QUEX_TKN_PREPROCESSOR_MULTILINE_END)
    //Write the non compilable tokens
    write_out(osstream, tokens_before_token)
    // Write the code
    if(token.type_id() != QUEX_TKN_BLOCK_OPEN && token.type_id() != QUEX_TKN_BLOCK_CLOSE)
        osstream << token.get_text().c_str()
    
    //Set the not_in_parentheses_block flag
    if(token.type_id() == QUEX_TKN_START_PARENTHESIS_BLOCK)
      not_in_parentheses_block = false
    else if(token.type_id() == QUEX_TKN_END_PARENTHESIS_BLOCK)
      not_in_parentheses_block = true
    
    //Check if it needs a semicolon
    if(
      tokenNext.type_id() != QUEX_TKN_BLOCK_OPEN &&  // avoid if();{
      token.type_id() != QUEX_TKN_PREPROCESSOR_MULTILINE_START &&  // avoid #define MACRO(A) do ;
      token.type_id() != QUEX_TKN_BLOCK_OPEN &&  // avoid  if(){;
      token.type_id() != QUEX_TKN_BLOCK_CLOSE &&  // avoid if(){};
      token.type_id() != QUEX_TKN_SEMICOLON &&  // avoid printf();;
      (is_in_buffer(tokens_before_tokenNext,QUEX_TKN_PREPROCESSOR_MULTILINE_CONTINUE) || // Need a "new line" to add a semicolon
       is_in_buffer(tokens_before_tokenNext,QUEX_TKN_NEWLINE) ) &&  // Need a new line to add a semicolon
      !is_in_buffer(tokens_before_tokenNext,QUEX_TKN_BACKSLASH) && // We can escape a semicolon
      multiline_continue &&  //do not add semicolon to the first preprocessor line
      not_in_parentheses_block
    )
      osstream << ";"

    //Is there another backslash? Should Someone care? FIXME
    if( is_in_buffer(tokens_before_tokenNext,QUEX_TKN_BACKSLASH))
      osstream << "\\"  //FIXME, only support one BACKSLASH, should print all of them!!
    
    //Check if we need to open or close a block
    if( tokenNext.type_id() == QUEX_TKN_BLOCK_OPEN && not_in_parentheses_block)
      osstream << "{"

    if(is_in_buffer(tokens_before_tokenNext,QUEX_TKN_PREPROCESSOR_MULTILINE_CONTINUE))
      multiline_continue = true

    //Need to print newlines and spaces after the block closing (looks better)
    if(tokenNext.type_id() == QUEX_TKN_BLOCK_CLOSE)
      print_many_block_close(osstream,qlex,token,tokenNext, tokens_before_token, tokens_before_tokenNext)
    else
      //Step forward one token
      token = tokenNext
      tokens_before_token.swap(tokens_before_tokenNext)
      get_token(tokenNext,qlex,tokens_before_tokenNext)

  if(tokenNext.type_id() == QUEX_TKN_BLOCK_CLOSE)
    print_many_block_close(osstream,qlex,token,tokenNext, tokens_before_token, tokens_before_tokenNext, true)
  
  return

//--helper--